//
//  PAHomePageView.m
//  PAMobileWallet
//
//  Created by Pay_SMP002 on 13-7-4.
//  Copyright (c) 2013年 平安付. All rights reserved.
//

#import "PAHomePageView.h"
#import "PublicDefine.h"

#import "PAHomePageViewController.h"
#import "PACellHomePage.h"
#import "PATimeLineInfo.h"
#import "UIColor+Extend.h"

#import <QuartzCore/QuartzCore.h>

#define HeightForCell 120

@implementation PAHomePageView
@synthesize homepageTable;
@synthesize addPhotoButton;
@synthesize toPersonalButton;
@synthesize payOneNewTrade;

@synthesize dataArray;
@synthesize noTradeView;
@synthesize noTradeBT;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        CGFloat x, y, w, h;
        
        x = 0;  y = 98/2 + 48/2;  w = 320;  h = iPhone5?(548-49-30+6):(460-49-30+6);
        
        homepageTable = [[UITableView alloc] initWithFrame:CGRectMake(x,y,w,h) style:UITableViewStylePlain];
        homepageTable.delegate = self;
        homepageTable.dataSource = self;
        homepageTable.backgroundView = nil;
        homepageTable.backgroundColor = [UIColor clearColor];
        homepageTable.showsHorizontalScrollIndicator = NO;
        homepageTable.showsVerticalScrollIndicator = NO;
        //setTableView.separatorStyle = UITableViewCellSeparatorStyleNone; //去掉横线
        [self addSubview:homepageTable];
        
        //回到个人中心 view
        x = 0;  y = 98/2;  w = 320;  h = 48/2;
        gotoPersonalView = [[UIView alloc] initWithFrame:CGRectMake(x, y, w, h)];
        gotoPersonalView.backgroundColor = [UIColor whiteColor];
        
        //回到个人中心 image
        UIImageView * gotoPersonalImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, w, h)];
        gotoPersonalImage.backgroundColor = [UIColor blackColor];
        gotoPersonalImage.image = [UIImage imageNamed:@"account_upordownbar.png"];
        [gotoPersonalView addSubview:gotoPersonalImage];

        //回到个人中心 button
        toPersonalButton = [UIButton buttonWithType:UIButtonTypeCustom];
        toPersonalButton.backgroundColor = [UIColor clearColor];
        toPersonalButton.frame = CGRectMake(0, 0, w, h);
        toPersonalButton.tag = 1;
        [toPersonalButton addTarget:self action:@selector(buttonActive:) forControlEvents:UIControlEventTouchDragInside|UIControlEventTouchDragOutside|UIControlEventTouchUpInside];
        [gotoPersonalView addSubview:toPersonalButton];
        [self addSubview:gotoPersonalView];
        
        // 表头的 view
        x = 0;  y = 98/2 + 48/2;  w = 320;  h = 90;
        headTableView = [[UIView alloc] initWithFrame:CGRectMake(x, y, w, h)];
        headTableView.backgroundColor = [UIColor whiteColor];
        headTableView.userInteractionEnabled = YES;

        // 点头像 付一笔新款
        x = 25;  y = (180-148)/2-6;  w = 148/2;  h = 148/2;
        addPhotoButton = [UIButton buttonWithType:UIButtonTypeCustom];
        addPhotoButton.backgroundColor = [UIColor clearColor];
        [addPhotoButton setBackgroundImage:[UIImage imageNamed:@"home_headPicAdd.png"] forState:UIControlStateNormal];
        addPhotoButton.frame = CGRectMake(x, y, w, h);
        addPhotoButton.tag = 2;
        [addPhotoButton addTarget:self action:@selector(buttonActive:) forControlEvents:UIControlEventTouchUpInside];
        [headTableView addSubview:addPhotoButton];
        
        // 点付一笔button 付一笔新款
        x = 320 - 74 - 25;  y = (90 - 40)/2;  w = 74;  h = 40;
        payOneNewTrade = [UIButton buttonWithType:UIButtonTypeCustom];
        payOneNewTrade.backgroundColor = [UIColor clearColor];
        payOneNewTrade.frame = CGRectMake(x, y, w, h);
        [payOneNewTrade setTitle:@"付一笔" forState:UIControlStateNormal];
        payOneNewTrade.tag = 3;
        [payOneNewTrade setTitleColor:[UIColor darkTextColor] forState:UIControlStateNormal];
        payOneNewTrade.titleLabel.font = [UIFont systemFontOfSize:20.0];
        [payOneNewTrade addTarget:self action:@selector(buttonActive:) forControlEvents:UIControlEventTouchUpInside];
        [headTableView addSubview:payOneNewTrade];
        
        UIImageView *homeLineIV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 88, 320, 1)];
        homeLineIV.image = [UIImage imageNamed:@"home_line.png"];
        [headTableView addSubview:homeLineIV];
        
        homepageTable.tableHeaderView = headTableView;

        dataArray = [[NSMutableArray alloc] initWithCapacity:10];
        
        x = 0;  y = 98/2 + 48/2 + 90;  w = 320;  h = iPhone5?(548-49-24-90):(460-49-24-90);
        noTradeView = [[UIView alloc] initWithFrame:CGRectMake(x, y, w, h)];
        noTradeView.backgroundColor = [UIColor whiteColor];
        [self addSubview:noTradeView];
        [noTradeView setHidden:YES];
        
        /*
        x = 0;  y = 0;  w = 320;  h = 1;
        UIImageView * noTradeLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, w, h)];
        noTradeLine.backgroundColor = [UIColor clearColor];
        noTradeLine.image = [UIImage imageNamed:@"public_line.png"];
        [noTradeView addSubview:noTradeLine];
        */
        
        x = (320 - 237/2)/2 +5;  y = 120/2;  w = 237/2;  h = 241/2;
        noTradeBT = [UIButton buttonWithType:UIButtonTypeCustom];
        noTradeBT.backgroundColor = [UIColor clearColor];
        noTradeBT.frame = CGRectMake(x, y, w, h);
        [noTradeBT setImage:[UIImage imageNamed:@"home_pic.png"] forState:UIControlStateNormal];
        [noTradeBT addTarget:self action:@selector(buttonActive:) forControlEvents:UIControlEventTouchUpInside];
        noTradeBT.enabled = NO;
        noTradeBT.tag = 4;
        [noTradeView addSubview:noTradeBT];

        x = 0;  y = 120/2 + 241/2 + 72/2;  w = 320;  h = 30;
        UILabel * noTradeLable = [[UILabel alloc] initWithFrame:CGRectMake(x, y, w, h)];
        noTradeLable.backgroundColor = [UIColor clearColor];
        noTradeLable.text = @"暂时没有交易记录";
        noTradeLable.textColor = [UIColor colorWithHexString:@"#acb0b6"];
        noTradeLable.textAlignment = NSTextAlignmentCenter;
        noTradeLable.font = [UIFont boldSystemFontOfSize:22.0];
        [noTradeView addSubview:noTradeLable];
    }
    return self;
}

- (IBAction)buttonActive:(id)sender
{
    [self.parentController testNext:sender];
}

- (void)refreshTradeHistory:(NSArray *)historyArray {
    self.dataArray = [NSMutableArray arrayWithArray:historyArray];
    
    if ([dataArray count] == 0){
        self.noTradeView.hidden = NO;
    }else{
        self.noTradeView.hidden = YES;
    }
    [self.homepageTable reloadData];
}

#pragma mark tab le view

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return 1;
}

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
    return [self.dataArray count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    return  HeightForCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    PACellHomePage * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (nil == cell)
    {
        cell = [[PACellHomePage alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        //cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.parentController = self.parentController;
    }
    UIView * view_bg = [[UIView alloc]initWithFrame:cell.frame];
    //view_bg.backgroundColor = [UIColor colorWithHexString:@"fc9322"];
    view_bg.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"home_hover.png"]];
    cell.selectedBackgroundView = view_bg;

    PATimeLineInfo *timeLineInfo = [self.dataArray objectAtIndex:indexPath.row];
    [cell loadInfo:timeLineInfo];

    CGFloat floatWide = [self setWideForString:cell.moneyNumLable.text];
    cell.moneyNumLable.frame = CGRectMake(267 - floatWide , 10 + (86 - 48)/2, floatWide , 48/2);
    cell.addOrCutImage.frame = CGRectMake(267 - floatWide - 48/2, 10 + (86 - 48)/2, 48/2, 48/2);

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"indexPath:%@",indexPath);
    if (self.parentController && [self.parentController respondsToSelector:@selector(selectHistoryTradeTargetIndexpath:)]) {
        [self.parentController performSelector:@selector(selectHistoryTradeTargetIndexpath:) withObject:indexPath];
    }
}

- (CGFloat)setWideForString:(NSString *)getString
{
    CGSize strSize = [getString sizeWithFont:[UIFont systemFontOfSize:18.0]];
    CGFloat lableWide = strSize.width + 10;
    return lableWide;
}

@end
